package com.hmkj.admin.controller.investor;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.po.amount.Account;
import com.hmkj.core.po.amount.Cash;
import com.hmkj.core.po.amount.Recharge;
import com.hmkj.core.po.investor.Investor;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.system.Bank;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.amount.AccountService;
import com.hmkj.core.service.amount.CashService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.core.service.investor.InvestorService;
import com.hmkj.core.service.system.AdminService;
import com.hmkj.core.service.system.BankService;
import com.hmkj.core.service.user.UserService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class InvestorController extends BaseController {

    @Resource
    InvestorService investorService;
    @Resource
    UserService userService;
    @Resource
    BankService bankService;
    @Resource
    AdminService adminService;
    @Resource
    RechargeService rechargeService;
    @Resource
    CashService cashService;
    @Resource
    AccountService accountService;

    /**
     * 投资人列表
     * @return
     */
    @RequestMapping(value= "/investor/list", method = RequestMethod.GET)
    public String list() {
        return ok("investor/list");
    }

    /**
     * 投资人列表
     *
     * @return
     */
    @RequestMapping("investor/detail")
    public String detail(ModelMap modelMap, Integer investorId) {

        // 投资人账户信息
        Investor investor = investorService.selectByPrimaryKey(investorId);
        modelMap.put("investor", investor);

        //投资人信息
        User investorUser = userService.selectByPrimaryKey(investor.getUserId());
        modelMap.put("user", investorUser);

        //投资人账户信息
        Account account = accountService.selectByPrimaryKey(investor.getUserId());
        modelMap.put("account", account);

        //保证金总存入
        List<Recharge> rechargeList = rechargeService.select(new Recharge(recharge -> {
            recharge.setUserId(investor.getUserId());
            recharge.setStatus(Recharge.STATUS.T3.code);
            recharge.setDelFlag(Recharge.DELFLAG.NORMAL.code);
        }));
        modelMap.put("totalRecharge",rechargeList.stream().mapToDouble(recharge -> null == recharge.getActualArrival() ? 0 : recharge.getActualArrival().doubleValue()).sum());


        //保证金总提取
        List<Cash> cashList = cashService.select(new Cash(cash -> {
            cash.setUserId(investor.getUserId());
            cash.setStatus(Cash.STATUS.T4.code);
            cash.setDelFlag(Recharge.DELFLAG.NORMAL.code);
        }));
        modelMap.put("totalCash",cashList.stream().mapToDouble(cash -> null == cash.getActualArrival() ? 0 : cash.getActualArrival().doubleValue()).sum());

        // 银行卡列表
        List<Bank> banks = bankService.select(new Bank(t->t.setDelFlag(GenericPo.DELFLAG.NORMAL.code)));
        modelMap.put("banks", banks);

        // 绑定的后台管理员列表
        List<Admin> admins = adminService.select(new Admin(admin -> {admin.setBussId(investor.getId());admin.setDelFlag(Admin.DELFLAG.NORMAL.code);}));
        modelMap.put("admins", admins);

        return ok("investor/detail");
    }
}